package org.jeecg.modules.generator.controller;

import cn.aresoft.excel.ExportExcel;
import cn.aresoft.excel.ImportExcel;
import cn.aresoft.excel.emuns.ExcelExportType;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.jeecg.modules.generator.entity.SysGenerateDatabaseConfig;
import org.jeecg.modules.generator.vo.SysGenerateDatabaseConfigVo;
import org.jeecg.modules.generator.service.SysGenerateDatabaseConfigService;
import org.jeecg.common.api.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.jeecg.common.aspect.annotation.AutoLog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
* @Description 数据源配置表控制器
* @Author yangfeng
* @Date 2021-06-29 17:28:26
*/
@RestController
@RequestMapping(value="generator/SysDatabaseConfig")
@Api(tags = "数据源配置表")
public class SysGenerateDatabaseConfigController {

    @Autowired
    private SysGenerateDatabaseConfigService sysGenerateDatabaseConfigService;

    @Value("${jeecg.path.upload}")
    private String upLoadPath;


    /**
    * 查询数据源配置表分页数据
    * @param sysGenerateDatabaseConfigVo
    * @return
    */
    @ApiOperation(value = "数据源配置表-分页查询")
    @AutoLog("数据源配置表-分页查询")
    @GetMapping(value="list")
    public Result<IPage<SysGenerateDatabaseConfig>> list(SysGenerateDatabaseConfigVo sysGenerateDatabaseConfigVo){

        Result<IPage<SysGenerateDatabaseConfig>> result = new Result<>();
        QueryWrapper queryWrapper = sysGenerateDatabaseConfigVo.getQueryWrapper();
            if(StringUtils.isNotEmpty(sysGenerateDatabaseConfigVo.getUsername())){
                queryWrapper.like("USERNAME",sysGenerateDatabaseConfigVo.getUsername());
            }
            if(StringUtils.isNotEmpty(sysGenerateDatabaseConfigVo.getName())){
                queryWrapper.like("NAME",sysGenerateDatabaseConfigVo.getName());
            }
            if(StringUtils.isNotEmpty(sysGenerateDatabaseConfigVo.getDatabaseType())){
                queryWrapper.eq("DATABASE_TYPE",sysGenerateDatabaseConfigVo.getDatabaseType());
            }
        IPage<SysGenerateDatabaseConfig> pageList = sysGenerateDatabaseConfigService.page(sysGenerateDatabaseConfigVo.getPage(),queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 根据主键查询数据源配置表对象数据
     * @return
     */
    @GetMapping(value="allList")
    public Result allList(){
        List<SysGenerateDatabaseConfig> sysGenerateDatabaseConfigList = sysGenerateDatabaseConfigService.list();
        return Result.ok(sysGenerateDatabaseConfigList);
    }

    /**
    * 根据主键查询数据源配置表对象数据
    * @param id
    * @return
    */
    @GetMapping(value="queryById/{id}")
    public Result queryById(@PathVariable("id")String id){

        SysGenerateDatabaseConfig sysGenerateDatabaseConfig = sysGenerateDatabaseConfigService.getById(id);
        if(sysGenerateDatabaseConfig != null){
           return Result.ok(sysGenerateDatabaseConfig);
        }else{
            return Result.error("未找到查询对象！");
        }
    }
    /**
    * 数据源配置表新增保存
    * @param sysGenerateDatabaseConfig
    * @return
    */
    @ApiOperation(value = "数据源配置表-新增保存")
    @AutoLog("数据源配置表-新增保存")
    @PostMapping(value="add")
    public Result add(@RequestBody SysGenerateDatabaseConfig sysGenerateDatabaseConfig){
        sysGenerateDatabaseConfigService.save(sysGenerateDatabaseConfig);
        return Result.ok("新增数据源配置表成功！");
    }

    /**
    * 数据源配置表修改保存
    * @param sysGenerateDatabaseConfig
    * @return
    */
    @ApiOperation(value = "数据源配置表-修改保存")
    @AutoLog("数据源配置表-修改保存")
    @PutMapping(value="edit")
    public Result edit(@RequestBody SysGenerateDatabaseConfig sysGenerateDatabaseConfig){

        SysGenerateDatabaseConfig generateDatabaseConfig = sysGenerateDatabaseConfigService.getById(sysGenerateDatabaseConfig.getId());
        if(generateDatabaseConfig != null){
            sysGenerateDatabaseConfigService.updateById(sysGenerateDatabaseConfig);
            return Result.ok("修改数据源配置表成功！");
        }else{
            return Result.error("修改数据源配置表不存在！");
        }
    }
    /**
    *数据源配置表删除
    * @param id
    * @return
    */

    @ApiOperation(value = "数据源配置表-删除")
    @AutoLog("数据源配置表-删除")
    @DeleteMapping(value="delete")
    public Result  delete(@RequestParam("id")String id){

        sysGenerateDatabaseConfigService.removeById(id);
        return Result.ok("删除数据源配置表数据成功！");
    }


    @ApiOperation(value = "数据源配置表-下载模板")
    @AutoLog("数据源配置表-下载模板")
    @GetMapping(value = "getExcelTemplate")
    public ResponseEntity<byte[]> getExcelTemplate(@RequestParam("templateName") String templateName){

        ExportExcel exportExcel = new ExportExcel(SysGenerateDatabaseConfig.class,templateName,"数据源配置表", ExcelExportType.TEMPLATE);
        exportExcel.saveExcelFileData(new ArrayList<>());
        return exportExcel.excelfileDownload();

    }


    @ApiOperation(value = "${generateEntity.tableDescripe}-导入数据")
    @AutoLog("${generateEntity.tableDescripe}-导入数据")
    @GetMapping(value = "importExcel")
    public Result  importExcel(@RequestParam("filepath") String filepath,@RequestParam("filename") String filename){

        try {
            String absPath = upLoadPath+ File.separator + filepath;
            ImportExcel importExcel = new ImportExcel(SysGenerateDatabaseConfig.class,absPath,filename,1);
            List<SysGenerateDatabaseConfig> dataList = new ArrayList<>();
            importExcel.getDataList(dataList);
            sysGenerateDatabaseConfigService.saveBatch(dataList);
            return Result.ok("导入成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("导入失败！");
        }
    }

    @ApiOperation(value = "${generateEntity.tableDescripe}-导出数据")
    @AutoLog("${generateEntity.tableDescripe}-导出数据")
    @GetMapping(value = "exportExcel")
    public ResponseEntity<byte[]>  exportExcel(SysGenerateDatabaseConfigVo sysGenerateDatabaseConfig){

        QueryWrapper queryWrapper = sysGenerateDatabaseConfig.getQueryWrapper();
        if(sysGenerateDatabaseConfig.getIdList() !=null && sysGenerateDatabaseConfig.getIdList().size()>0){
            queryWrapper.in("ID",sysGenerateDatabaseConfig.getIdList());
        }
        Page<SysGenerateDatabaseConfig> page  = new Page(1,500000);
        page.setSearchCount(false);
        IPage<SysGenerateDatabaseConfig> dataList = sysGenerateDatabaseConfigService.page(page, queryWrapper);
        ExportExcel exportExcel = new ExportExcel(SysGenerateDatabaseConfig.class,"数据源配置表.xlsx","数据源配置表", ExcelExportType.DATA);
        exportExcel.saveExcelFileData(dataList.getRecords());
        return exportExcel.excelfileDownload();

    }



}
